"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useReport = exports.default = void 0;

var _isServer = _interopRequireDefault(require("./is-server"));

var _browser = _interopRequireDefault(require("./browser"));

var _react = _interopRequireDefault(require("react"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var reportTaskList = function reportTaskList() {
  if (process.env.DISCUZ_ENV === "web" && process.env.NODE_ENV !== "development" && !(0, _isServer.default)()) {
    var beaconTaskListArray = Object.values(window.beaconUITaskList);

    if (beaconTaskListArray && beaconTaskListArray.length > 0) {
      for (var i = 0; i < beaconTaskListArray.length; i++) {
        window.beaconUI.onUserAction("componentInit", _objectSpread({}, beaconTaskListArray[i]));
      }

      window.beaconUITaskList = {};
    }
  }
};

var registerTask = function registerTask(reportData) {
  var componentName = reportData.name;

  if (!window.beaconUITaskList) {
    window.beaconUITaskList = _defineProperty({}, componentName, reportData);
  } else {
    if (window.beaconUITaskList[componentName]) {
      window.beaconUITaskList[componentName].ct += 1;
    } else {
      window.beaconUITaskList[componentName] = reportData;
    }
  }
};

function initTj() {
  if (process.env.DISCUZ_ENV === "web" && process.env.NODE_ENV !== "development" && !(0, _isServer.default)() && !window.BeaconAction) {
    window.beaconIsLoading = true;
    var tj = document.createElement("script");
    tj.src = "https://beaconcdn.qq.com/sdk/4.3.4/beacon_web.min.js";

    tj.onload = function () {
      window.beaconUI = new BeaconAction({
        appkey: "0WEB0U4I4M4V73SR",
        // 系统或项目id, 必填
        delay: 3000
      });
      window.beaconUI.addAdditionalParams({
        host: window.location.host,
        path: window.location.pathname,
        pfm: _browser.default.env("mobile") ? "h5" : "pc"
      });
      reportTaskList();
    };

    document.body.appendChild(tj);
  }
}

var report = function report(_ref) {
  var componentName = _ref.componentName,
      _ref$pa = _ref.pa,
      pa = _ref$pa === void 0 ? "" : _ref$pa,
      _ref$pn = _ref.pn,
      pn = _ref$pn === void 0 ? "" : _ref$pn;

  if (process.env.DISCUZ_ENV === "web") {
    var reportData = {
      name: componentName,
      pa: pa,
      pn: pn,
      ct: 1
    }; // server ssr 不上报

    if (!(0, _isServer.default)()) {
      if (!window.beaconUI) {
        if (!window.beaconIsLoading) {
          initTj();
        }

        registerTask(reportData);
      } else {
        registerTask(reportData);
        setTimeout(function () {
          reportTaskList();
        }, 1000);
      }
    }
  }
};

var useReport = function useReport(config) {
  return _react.default.useEffect(function () {
    report(config);
  }, []);
};

exports.useReport = useReport;
var _default = report;
exports.default = _default;